package logic

import (
	"context"
	"errors"
	"home-view/models/mysql"
	"strings"

	"home-view/homerpc/homerpc"
	"home-view/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *homerpc.CommentAddRequest) (*homerpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if strings.Contains(in.Content, "欺诈") || strings.Contains(in.Content, "伪劣") {
		return nil, errors.New("评价内容中含有敏感词")
	}

	comment := mysql.Comment{
		UserId:  in.UserId,
		ClassId: in.ClassId,
		Content: in.Content,
	}

	if err := comment.CommentAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &homerpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
